package tech.dx.springdb.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tech.dx.springdb.entity.CourseAdviceType;
import tech.dx.springdb.service.CourseAdviceTypeService;

import java.util.Optional;

/**
 * @ClassName CourseAdviceTypeController
 * @Description TODO
 * @Author daoxuan
 * @Date 2019/4/2 17:51
 * @Version 1.0
 */
@RestController
@RequestMapping("courseadvicetype")
@Api(tags = {"课程建议类型"})
public class CourseAdviceTypeController {

    @Autowired
    private CourseAdviceTypeService courseAdviceTypeService;

    @GetMapping("/{id}")
    public CourseAdviceType get(@PathVariable long id) {
        Optional<CourseAdviceType> byId = courseAdviceTypeService.getById(id);
        if (byId.isPresent()) {
            return byId.get();
        } else {
            return null;
        }
    }

    @PostMapping
    public CourseAdviceType add(@RequestBody CourseAdviceType courseAdviceType) {
        return courseAdviceTypeService.add(courseAdviceType);
    }

    @PutMapping("/{id}")
    public CourseAdviceType update(@RequestBody CourseAdviceType courseAdviceType, @PathVariable long id) {
        if (courseAdviceTypeService.isExitById(id)) {
            courseAdviceType.setId(id);
            return courseAdviceTypeService.update(courseAdviceType);
        } else {
            return null;
        }
    }

    @DeleteMapping("/{id}")
    public void del(@PathVariable long id) {
        if (courseAdviceTypeService.isExitById(id)) {
            courseAdviceTypeService.del(id);
        }
    }
}
